<?php
/**
 * 
 * @author guibinyu
 * @date 2017-06-15 04:14:50
 */
class Mass   
{
    
   private $params;
   private $Wechat;
   private $users;
   public function __construct($Wechat)
    {
        $this->Wechat = $Wechat;
        $user_type = 1;
        if($user_type ==1 )
        {
            $this->users = array('touser'=>array("oYVyYw2BzLX8eJ1I-oCDSN6z4avI",'oYVyYw5CWLb7YwSIHGZ2gZGpcwVc'));
            $result = $this->Wechat->User->getUserList();
            if(isset($result['data']['openid']) && is_array($result['data']['openid'])){
                $this->users['touser'] = $result['data']['openid'];
            }
        }
        else
        {
            $this->users = array('filter' => array('is_to_all'=>true));
        }
    }
    
    public function sendtext($send=true)
    {
      
        $text = "这是群发消息";
        $send = true;
     
        $result = $this->Wechat->MassMessage->setToUser($this->users)->text($text)->send($send);
        return $result;
    }
    public function sendimage($send=true)
    {
        $media_id = "tm0D20ZrIxNCja3xm6dVBTCoYwn-OVaprhMAImBIoGA";
        $result = $this->Wechat->MassMessage->setToUser($this->users)->image($media_id)->send($send);
        return $result;
    }
     public function sendvoice($send=true)
    {
        $media_id = "tm0D20ZrIxNCja3xm6dVBUfveZgQA7LwcN5V8_L3-zE";
        $result = $this->Wechat->MassMessage->setToUser($this->users)->voice($media_id)->send($send);
        return $result;
    }
    /**
     * 需要经过media/media上传素材
     * 再用media/uploadMpVideo
     */
     public function sendvideo($send=true)
    {
         
        $video = array(
                'media_id'     => "JMPnkAzL14MyfmnVacj2vJRPGDcD7AKWMhocdolzXgSAzJO47I5HD0y1G-LADj9W",
//                'thumb_media_id'  => $thumb_media_id,
                'title' => '群发消息标题xx',
                'description'=> '群发消息描述xx',
            );
        $result = $this->Wechat->MassMessage->setToUser($this->users)->video($video)->send($send); 
        return $result;
    }
    public function sendmpnews($send=true)
    {
        
       $media_id = "Ij9hAhnHpqGF8ogBA3z01utIfb57-Ww5pJBSZD2cMjChTK9eur3tmQwQ92XC-bBz";
        $result = $this->Wechat->MassMessage->setToUser($this->users)->mpnews($media_id)->send($send);
        return $result;
    }
    /**
     * 获取菜单
     */
    public function del()
    {
        $msg_id = '1000000007';
        $result = $this->Wechat->MassMessage->del($msg_id);
        return $result;
    }
    /**
     * 获取菜单
     */
    public function preview($s3)
    {
        $this->users = array('touser'=>array('oYVyYw2BzLX8eJ1I-oCDSN6z4avI'));
        $send = FALSE;
        $function_name = isset($_POST['param']) && !empty($_POST['param']) ? json_decode($_POST['param'],true) : 'sendtext';
        if(function_exists($function_name))
        {
            $data = $this->$function_name($send);
        };
//        $data = $this->sendtext($send);
        
        $result = $this->Wechat->MassMessage->preview($data);
        return $result;
    }
     /**
     * 获取菜单
     */
    public function query()
    {
        $result = $this->Wechat->MassMessage->query();
        return $result;
    }
//index, view, create, update, delete, options
}